package yaohua.jsoup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import yaohua.main.BorrowItemActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;

public class Jsoup_help
	{
		/**
		 * constructor
		 */

		public Jsoup_help()
			{
			}

		/**
		 * 通过html的字符串获得Document
		 * 
		 * @param string
		 * @return
		 */

		public Document getDocument(String string)
			{
				Document doc = Jsoup.parse(string);
				return doc;
			}

		/**
		 * 通过class来确定Elements
		 * 
		 * @param doc
		 * @param string
		 * @return
		 */

		public List<String> geTextListByClass(Document doc, String string)
			{
				List<String> list = new ArrayList<String>();
				Elements elements = doc.getElementsByClass(string);
				for (Element s : elements)
					{
						list.add(s.text());
					}
				return list;
			}
		/**
		 * 没有馆藏的情况下显示书籍的基本信息(预约和超期书籍的基本信息)
		 * 
		 * @param result
		 * @param tabel
		 * @param number
		 * @param position
		 * @param list
		 * @param from
		 * @param to
		 */

		public void handlerItem(String result, String tabel, int number, int position, ArrayList<String> list, Context from,
				Class<?> to)
			{

				// 测试Jsoup的方法
				Document doc = getDocument(result);
				// 得到标题的list
				List<String> listTitle = geTextListByClass(doc, tabel);
				// 创建带有Map的List，用于传送给ItemActivity
				List<Map<String, String>> listItem = new ArrayList<Map<String, String>>();
				// 向listItem中装入map
				for (int i = position * number, j = 0; i < (i + 1) * number && j < number; i++, j++)
					{
						Map<String, String> map = new HashMap<String, String>();
						map.put("title", listTitle.get(j));
						map.put("value", (String) list.get(i));
						listItem.add(map);
					}

				// 向ItemActivity跳转
				Intent intent = new Intent();
				intent.putParcelableArrayListExtra("listItem", (ArrayList<? extends Parcelable>) listItem);
				intent.setClass(from, to);
				from.startActivity(intent);

			}

		/**
		 * 带有馆藏信息或者可以预约的情况下显示数据的具体信息
		 * 
		 * @param result
		 * @param tabel
		 * @param number
		 * @param position
		 * @param list
		 * @param storeUrl
		 * @param from
		 * @param to
		 */

		public void handlerItem(String result, String tabel, int number, int position, ArrayList<String> list,
				ArrayList<String> url, Context from, Class<?> to)
			{

				// 测试Jsoup的方法
				Document doc = getDocument(result);
				// 得到标题的list
				List<String> listTitle = geTextListByClass(doc, tabel);
				// 创建带有Map的List，用于传送给ItemActivity
				List<Map<String, String>> listItem = new ArrayList<Map<String, String>>();
				// 向listItem中装入map
				for (int i = position * number, j = 0; i < (i + 1) * number && j < number; i++, j++)
					{
						Map<String, String> map = new HashMap<String, String>();
						map.put("title", listTitle.get(j));
						map.put("value", (String) list.get(i));
						listItem.add(map);
					}

				// 向ItemActivity跳转
				Intent intent = new Intent();
				intent.putExtra("position", position);
				intent.putStringArrayListExtra("url", url);
				intent.putParcelableArrayListExtra("listItem", (ArrayList<? extends Parcelable>) listItem);
				intent.setClass(from, to);
				from.startActivity(intent);

			}

		/**
		 * 获得搜索结果页面书籍的链接地址
		 * 
		 * @param url
		 * @return
		 */
		public ArrayList<String> getStoreUrl(String html)
			{
				ArrayList<String> storeUrl = new ArrayList<String>();
				Document doc = Jsoup.parse(html, "http://202.207.182.100:8080/pft/wxjs/tmjs_form.asp");
				Elements elements = doc.select("a[href~=../showmarc/.*?nTmpKzh=\\d*]");
				for (Element link : elements)
					{
						storeUrl.add(link.attr("abs:href"));
					}
				return storeUrl;
			}
		/**
		 * 获取续借的url
		 * @param html
		 * @return
		 */
		public ArrayList<String> getRenewUrl(String html)
		{
			ArrayList<String> renewUrl =new ArrayList<String>();
			Document doc=Jsoup.parse(html,"http://202.207.182.100:8080/pft/dzjs/login_form.asp");
			Elements elements=doc.select("a[href~=../dzxj/dzxj.asp\\?nbsl=\\d*]");
			for(Element link: elements)
				{
					renewUrl.add(link.attr("abs:href"));
				}
			return renewUrl;
		}
	}
